<script setup>
import { ref, onMounted, computed } from 'vue';

// 课程数据
const courses = ref([]);
const searchTerm = ref('');
const selectedCategory = ref('全部');

// 初始化课程数据
onMounted(() => {
  // 模拟课程数据，实际应用中可以从API获取
  courses.value = [
    {
      id: 1,
      title: "人工智能与创意设计",
      description: "学习如何使用人工智能与创意设计创建令人惊叹的艺术作品",
      image: "https://imagecgr.oss-cn-beijing.aliyuncs.com/ai.png",
      rating: 4.8,
      reviews: 342,
      category: "人工智能生成",
      level: "初级",
      duration: "10小时",
    },
    {
      id: 2,
      title: "ChatGPT提示工程",
      description: "掌握为大型语言模型创建有效提示的艺术，获得更好的结果",
      image: "https://imagecgr.oss-cn-beijing.aliyuncs.com/daolun.png",
      rating: 4.9,
      reviews: 521,
      category: "提示工程",
      level: "中级",
      duration: "8小时",
    },
    {
      id: 3,
      title: "使用Next.js构建AI应用",
      description: "使用Next.js和OpenAI创建全栈AI驱动的Web应用",
      image: "https://imagecgr.oss-cn-beijing.aliyuncs.com/jiegou.jpg",
      rating: 4.7,
      reviews: 218,
      category: "开发",
      level: "高级",
      duration: "15小时",
    },
    {
      id: 5,
      title: "机器学习入门",
      description: "面向初学者的机器学习概念和应用介绍",
      image: "/static/placeholder.png",
      rating: 4.5,
      reviews: 412,
      category: "机器学习",
      level: "初级",
      duration: "8小时",
    },
  ];
});

// 计算属性：过滤后的课程
const filteredCourses = computed(() => {
  return courses.value.filter(course => {
    const matchesSearch =
      course.title.toLowerCase().includes(searchTerm.value.toLowerCase()) ||
      course.description.toLowerCase().includes(searchTerm.value.toLowerCase());
    const matchesCategory = selectedCategory.value === '全部' || course.category === selectedCategory.value;
    return matchesSearch && matchesCategory;
  });
});

// 计算属性：课程分类列表
const categories = computed(() => {
  const uniqueCategories = [...new Set(courses.value.map(course => course.category))];
  return ['全部', ...uniqueCategories];
});

// 生成星级评分
const getStars = (rating) => {
  const stars = [];
  for (let i = 1; i <= 5; i++) {
    if (i <= Math.floor(rating)) {
      stars.push('full');
    } else if (i <= rating) {
      stars.push('half');
    } else {
      stars.push('empty');
    }
  }
  return stars;
};

// 当前年份
const currentYear = new Date().getFullYear();

// 查看课程详情
const viewCourse = (course) => {
  uni.navigateTo({
    url: `/pages/OnlineClass/OnlineClass?courseId=${course.id}`
  });
};
</script>

<template>
  <view class="container">
    <!-- 搜索和筛选 -->
    <view class="search-section">
      <view class="search-container">
        <view class="filter-box">
          <picker :range="categories" @change="e => selectedCategory = categories[e.detail.value]"
            class="category-picker">
            <view class="picker-view">
              <text>{{ selectedCategory }}</text>
              <view class="dropdown-icon">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none"
                  stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                  <polyline points="6 9 12 15 18 9"></polyline>
                </svg>
              </view>
            </view>
          </picker>
        </view>
      </view>
    </view>

    <!-- 课程列表 - 单列瀑布流 -->
    <view class="course-section">
      <text class="section-title">精选课程</text>

      <view class="course-list">
        <block v-if="filteredCourses.length > 0">
          <view v-for="course in filteredCourses" :key="course.id" class="course-card">
            <view class="course-image-container">
              <image :src="course.image" class="course-image" mode="aspectFill"></image>
              <text class="course-category">{{ course.category }}</text>
            </view>
            <view class="course-content">
              <view class="course-header">
                <text class="course-title">{{ course.title }}</text>
              </view>
              <text class="course-description">{{ course.description }}</text>
              <view class="course-rating">
                <view class="stars">
                  <view v-for="(star, index) in getStars(course.rating)" :key="index" class="star">
                    <svg v-if="star === 'full'" xmlns="http://www.w3.org/2000/svg" width="20" height="20"
                      viewBox="0 0 24 24" fill="#FBBF24" stroke="#FBBF24" stroke-width="1.5" stroke-linecap="round"
                      stroke-linejoin="round">
                      <polygon
                        points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2">
                      </polygon>
                    </svg>
                    <svg v-else-if="star === 'half'" xmlns="http://www.w3.org/2000/svg" width="20" height="20"
                      viewBox="0 0 24 24" fill="#FBBF24" stroke="#FBBF24" stroke-width="1.5" stroke-linecap="round"
                      stroke-linejoin="round">
                      <path d="M12 17.8 5.8 21 7 14.1 2 9.3 8.9 8.3 12 2"></path>
                      <path d="M12 2v15.8" fill="none"></path>
                    </svg>
                    <svg v-else xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"
                      fill="none" stroke="#D1D5DB" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
                      <polygon
                        points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2">
                      </polygon>
                    </svg>
                  </view>
                </view>
                <text class="rating-text">{{ course.rating }} ({{ course.reviews }} 评论)</text>
              </view>
              <view class="course-tags">
                <text class="tag">{{ course.level }}</text>
                <text class="tag">{{ course.duration }}</text>
              </view>
              <button class="enroll-btn" @click="viewCourse(course)">查看课程</button>
            </view>
          </view>
        </block>
        <view v-else class="no-courses">
          <text class="no-courses-text">没有找到符合您条件的课程。</text>
        </view>
      </view>
    </view>

    <!-- 页脚 -->
    <view class="footer">
      <view class="footer-content">
        <view class="footer-section">
          <text class="footer-title">AIGC学院</text>
          <text class="footer-text">为创作者提供前沿AI教育和资源。</text>
        </view>
        <view class="footer-section">
          <text class="footer-title">快速链接</text>
          <view class="footer-links">
            <text class="footer-link">首页</text>
            <text class="footer-link">课程</text>
            <text class="footer-link">资源</text>
            <text class="footer-link">关于我们</text>
          </view>
        </view>
        <view class="footer-section">
          <text class="footer-title">订阅</text>
          <text class="footer-text">获取我们最新的课程和AI新闻更新。</text>
          <view class="subscribe-form">
            <input type="text" class="subscribe-input" placeholder="您的邮箱" />
            <button class="subscribe-btn">订阅</button>
          </view>
        </view>
      </view>
      <view class="footer-bottom">
        <text class="copyright">© {{ currentYear }} AIGC学院. 保留所有权利。</text>
      </view>
    </view>
  </view>
</template>

<style>
/* 全局样式 */
.container {
  min-height: 100vh;
  background-color: #f9fafb;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

/* 头部导航 */
.header {
  background-color: #2563eb;
  padding: 15px 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  color: #ffffff;
  font-size: 20px;
  font-weight: bold;
}

.nav-links {
  display: none;
}

.nav-link {
  color: #ffffff;
  margin: 0 15px;
  font-size: 16px;
}

.login-btn {
  background-color: #ffffff;
  color: #2563eb;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
}

/* 英雄区域 */
.hero {
  background-color: #1d4ed8;
  padding: 40px 20px;
  text-align: center;
}

.hero-content {
  max-width: 800px;
  margin: 0 auto;
}

.hero-title {
  color: #ffffff;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 15px;
  display: block;
}

.hero-subtitle {
  color: #bfdbfe;
  font-size: 16px;
  line-height: 1.5;
  display: block;
}

/* 搜索和筛选 */
.search-section {
  background-color: #ffffff;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.search-container {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.search-box {
  position: relative;
  flex: 1;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #9ca3af;
}

.search-input {
  width: 100%;
  padding: 10px 10px 10px 40px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
}

.filter-box {
  width: 100%;
}

.category-picker {
  width: 100%;
}

.picker-view {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background-color: #ffffff;
}

.dropdown-icon {
  color: #6b7280;
}

/* 课程列表 */
.course-section {
  padding: 30px 20px;
}

.section-title {
  font-size: 24px;
  font-weight: bold;
  color: #1f2937;
  text-align: center;
  margin-bottom: 25px;
  display: block;
}

.course-list {
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.course-card {
  background-color: #ffffff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  transition: box-shadow 0.3s ease;
}

.course-card:active {
  box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
}

.course-image-container {
  position: relative;
  height: 180px;
  width: 100%;
}

.course-image {
  height: 100%;
  width: 100%;
}

.course-category {
  position: absolute;
  top: 15px;
  right: 15px;
  background-color: #2563eb;
  color: #ffffff;
  padding: 5px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.course-content {
  padding: 20px;
}

.course-header {
  margin-bottom: 10px;
}

.course-title {
  font-size: 18px;
  font-weight: bold;
  color: #1f2937;
  display: block;
}

.course-description {
  font-size: 14px;
  color: #6b7280;
  margin-bottom: 15px;
  line-height: 1.5;
  display: block;
}

.course-rating {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.stars {
  display: flex;
  margin-right: 10px;
}

.star {
  margin-right: 2px;
}

.rating-text {
  font-size: 14px;
  color: #6b7280;
}

.course-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 15px;
}

.tag {
  background-color: #dbeafe;
  color: #1e40af;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.enroll-btn {
  width: 100%;
  background-color: #2563eb;
  color: #ffffff;
  padding: 10px 0;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  text-align: center;
}

.no-courses {
  text-align: center;
  padding: 40px 0;
}

.no-courses-text {
  font-size: 16px;
  color: #6b7280;
}

/* 页脚 */
.footer {
  background-color: #1e3a8a;
  padding: 40px 20px 20px;
  color: #ffffff;
}

.footer-content {
  display: flex;
  flex-direction: column;
  gap: 30px;
  margin-bottom: 30px;
}

.footer-section {
  margin-bottom: 20px;
}

.footer-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 15px;
  display: block;
}

.footer-text {
  color: #bfdbfe;
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 15px;
  display: block;
}

.footer-links {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.footer-link {
  color: #bfdbfe;
  font-size: 14px;
}

.subscribe-form {
  display: flex;
  margin-top: 15px;
}

.subscribe-input {
  flex: 1;
  padding: 10px;
  border-top-left-radius: 6px;
  border-bottom-left-radius: 6px;
  border: none;
  font-size: 14px;
}

.subscribe-btn {
  background-color: #2563eb;
  color: #ffffff;
  padding: 10px 15px;
  border-top-right-radius: 6px;
  border-bottom-right-radius: 6px;
  font-size: 14px;
  font-weight: 500;
}

.footer-bottom {
  border-top: 1px solid #3b82f6;
  padding-top: 20px;
  text-align: center;
}

.copyright {
  color: #93c5fd;
  font-size: 14px;
}

/* 媒体查询 - 平板和桌面 */
@media screen and (min-width: 768px) {
  .nav-links {
    display: flex;
  }

  .search-container {
    flex-direction: row;
  }

  .filter-box {
    width: 200px;
  }

  .footer-content {
    flex-direction: row;
    justify-content: space-between;
  }

  .footer-section {
    width: 30%;
  }
}
</style>